package com.way.filemanager.activities;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.way.filemanager.R;
import com.way.filemanager.navigation.MyAdapter;
import com.way.filemanager.navigation.MyFileManager;
import com.way.filemanager.navigation.MyFileManager.FileChangeListener;
import com.way.filemanager.navigation.MyFileManager.FileProgressListener;
import com.way.filemanager.navigation.NavigationListFragmant.Mode;
import com.way.filemanager.navigation.NavigationListFragmant.SelectMode;
import com.way.filemanager.util.CommonFunc;

import java.io.File;
import java.util.LinkedList;

import hwdroid.app.HWActivity;
import hwdroid.widget.ActionBar.ActionBarView;
import hwdroid.widget.ActionBar.ActionBarView.OnBackKeyItemClick;
import hwdroid.widget.FooterBar.FooterBarButton;
import hwdroid.widget.FooterBar.FooterBarType.OnFooterItemClick;

public class GetContextActivity extends HWActivity implements FileProgressListener, FileChangeListener{
    private static final String TAG = "GetContextActivity";
    private final static int MENU_ID_NO = 1;
    private final static int MENU_ID_YES = 2;
    private MyFileManager fm;
    private MyAdapter mAdapter;
    //private FooterBarMenu mFooterBarMenu;
    private FooterBarButton mFooterBarButton;
    private Activity mActivity;
    private ViewGroup mfootbar_menu_id;
    private ViewGroup mDirContainer;
    private View mTagView;
    private HorizontalScrollView mTagScroll;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle state) {
        super.onCreate(state);
        Log.v(TAG, "FileManagerAppFrame oncreate");
        mActivity = this;
        LayoutInflater mInflater = LayoutInflater.from(mActivity);
        ViewGroup v = (ViewGroup) mInflater.inflate(R.layout.navigation_list, null, false);
        setActivityContentView(v);

        setTitle(R.string.app_name);

        fm = new MyFileManager(mActivity);
        fm.goRootFolder();
        Intent mresultCode = getIntent();
        Log.v("rwei", "mresultCode:" +mresultCode);
        initFooterBarMenu();

        mfootbar_menu_id = (ViewGroup) findViewById(R.id.natigation_footbar_menu);
        mfootbar_menu_id.addView(mFooterBarButton);

        mAdapter = new MyAdapter(mActivity, v,fm);
        fm.setFileChangeListener(mAdapter);

        mAdapter.setLongClickFileListener(null);
        mAdapter.setFileChangeListener(this);

        mAdapter.setSearchButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        mTagView = v.findViewById(R.id.navigation_dir_id);
        mDirContainer = (LinearLayout) v.findViewById(R.id.dir_container);
        mDirContainer.removeAllViews();
        mTagScroll = (HorizontalScrollView) v.findViewById(R.id.horizontalScrollView1);

        ensureSelectView();
        mAdapter.setMode(Mode.getcontext);
        mAdapter.setFooterBarSelectButton(mFooterBarButton.getItem(MENU_ID_YES));
        ActionBar bar = mActivity.getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        bar.setCustomView(mSelectView);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);
        bar.setDisplayShowCustomEnabled(true);

        //mTagView.setVisibility(View.GONE);
    }
    private ActionBarView mSelectView;
    private void ensureSelectView()
    {
        if(null == mSelectView)
        {
            mSelectView = new ActionBarView(mActivity);
            /*mSelectView.showBackKey(true, new OnBackKeyItemClick()
            {
                @Override
                public void onBackKeyItemClick()
                {
                    fm.goUpFolder();
                }
            });
           */
            mSelectView.setTitle(mActivity.getResources().getQuantityString(R.plurals.numberOfSelected, 1, 0));
            //CheckBox cb = new CheckBox(mActivity);
            //cb.setId(R.id.checkbox_select_all);
            //mSelectView.addRightItem(cb);

            mAdapter.setActionBarView(mSelectView);
            //mAdapter.setSelectAll(cb);

        }
    }
    @Override
    public void onChanged()
    {
        updateTopTag();
    }

    @Override
    public void onPreProgressStart(SelectMode mode) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgress(File f, int p, int total) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressDone() {
        // TODO Auto-generated method stub

    }

    private void initFooterBarMenu()
    {
        if(null == mFooterBarButton)
        {
            Resources r = mActivity.getResources();
            mFooterBarButton = new FooterBarButton(mActivity);
            mFooterBarButton.addItem(MENU_ID_NO, r.getText(R.string.cancel),null);
            mFooterBarButton.addItem(MENU_ID_YES, r.getText(R.string.ok),null);
            mFooterBarButton.getItem(MENU_ID_YES).setEnabled(false);
            mFooterBarButton.setOnFooterItemClick(new OnFooterItemClick()
            {
                @Override
                public void onFooterItemClick(View view, int id)
                {
                    switch(id)
                    {
                        case MENU_ID_NO:
                            finish();
                            break;
                        case MENU_ID_YES:
                            LinkedList<File> files = mAdapter.getSelectedFiles();
                            if(files.size()>=1)
                            {
                                Intent mIntent = new Intent();
                                mIntent.setData(Uri.fromFile(files.get(0)));
                                setResult(Activity.RESULT_OK, mIntent);
                                finish();
                            }
                            break;
                        default:
                            break;
                    }
                }
            });

            mFooterBarButton.setPrimaryItemCount(3);
            mFooterBarButton.updateItems();
        }
    }
    private View getTagView(String tag, String folderPath)
    {
        ViewGroup v = (LinearLayout)LayoutInflater.from(mActivity).inflate(R.layout.crumbs_item, null);
        v.setBackgroundResource(R.drawable.aui_crumb_bg_main);
        v.setClickable(true);
        ((TextView)v.findViewById(R.id.dir_name)).setText(tag);

        final File folder = new File(folderPath);
        v.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                fm.openFolder(folder);
            }
        });
        return v;
    }
    private void updateTopTag()
    {
        mDirContainer.removeAllViews();

        LinearLayout ItemMain = (LinearLayout) LayoutInflater.from(mActivity).inflate(
                R.layout.crumbs_item, null);
        ItemMain.setBackgroundResource(R.drawable.aui_crumb_bg_main);
        ItemMain.setClickable(true);
        TextView titleMain = (TextView) ItemMain.findViewById(R.id.dir_name);
        titleMain.setText(mActivity.getString(R.string.AllFiles));
        mDirContainer.addView(ItemMain);
        ItemMain.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fm.goRootFolder();
            }
        });

        String rootName = fm.getRootName();
        String rootPath = fm.getRootPath();
        String currentDir = fm.getCurrentDir().getAbsolutePath();
        if(currentDir.startsWith(rootPath))
        {
            String path = currentDir.substring(rootPath.length());
            String[] tags = path.split(File.separator);

            if(rootName != null && rootName.length() > 0)
            {
                mDirContainer.addView(getTagView(rootName, rootPath));
            }

            for(String tag : tags)
            {
                if(tag == null || tag.length() <= 0)
                {
                    continue;
                }
                rootPath += File.separator + tag;
                mDirContainer.addView(getTagView(tag, rootPath));
            }
        }
        else
        {
            fm.goUpFolder();
        }
        mHandler.removeCallbacks(mTagScrollRun);
        mHandler.post(mTagScrollRun);
    }
    private Runnable mTagScrollRun = new Runnable(){ @Override public void run()
    {
        if(mTagScroll != null)
        {
            mTagScroll.fullScroll(View.FOCUS_RIGHT);
        }
    }};
    @Override
    public void onProgress(File f, long currentbytes, long totalbytes) {
		// TODO Auto-generated method stub

    }
}
